
' This example shows the two different ways to use the keyboard.
' You can move Bob with the arrow keys (make sure to click on the game to focus
' it first)

Type Tsquare
	Var x as number
	Var y as number
	Var dx as number
	Var dy as number
	Var w as number
	Var h as number
EndType

'This is bob. It is a type with some values inside
Var bob as Tsquare
    bob.x = 400
    bob.y = 280
    bob.dx = 0
    bob.dy = 0
    bob.w = 50
    bob.h = 50

Function OnUpdate(dt as number)
' OnUpdate is where the game intelligence goes. In this case
' we use it to change Bobs internal dy variable by calling
' KeyDown
    bob.dy = 0
    If KeyDown("up") Then
    	bob.dy = -1
    EndIF
    If KeyDown("down") Then
    	bob.dy = 1
    EndIf
    bob.x = bob.x + bob.dx * 60 * dt
    bob.y = bob.y + bob.dy * 60 * dt
EndFunction

Function OnKeyPressed(key as string, code as number)
' OnPressed gets called every time a key is pressed. In this
' case we use it to change bobs dx variable
	If key = "left" Then
		bob.dx = -1
	EndIF
	If key = "right" Then
		bob.dx = 1
	EndIf	
EndFunction

Function OnKeyReleased(key as string, code as number)
' OnReleased gets called every time a key is released. In this
' case we use it to change bobs dx variable
	If key = "left" and bob.dx = -1 or key = "right" and bob.dx = 1 Then
		bob.dx = 0
	EndIf

EndFunction

Function OnDraw()
' This big function draws bob as a series of circles, squares and arcs
	setColor(255,255,0)
	Var l as number
	l = bob.x - bob.w/2
	Var t as number
	t = bob.y - bob.h/2
	Var w as number
	w = bob.w
	Var h as number
	h = bob.h
	FillRectangle(l,t,w,h)

	setColor(128,128,0)
	setLineWidth(3)
	StrokeRectangle(l,t,w,h)

	setColor(255,255,255)

	Var r as number
	r = w/5
	Var r2 as number
	r2 = w/13

	Var x1 as number
	Var y as number
	Var x2 as number
	x1 = l + r + r2
	y = t + r + r2
	x2 = l + w - r - r2

	FillCircle(x1, y, r)
	FillCircle(x2, y, r)

	setColor(0,0,0)
	setLineWidth(1)
	StrokeCircle(x1,y,r)
	FillCircle(x1 + bob.dx * r2,y + bob.dy * r2,r2)
	StrokeCircle(x2,y,r)
	FillCircle(x2 + bob.dx * r2,y + bob.dy * r2,r2)

	StrokeCircle(bob.x, bob.y, r2)
	StrokeArc(bob.x, bob.y, 2*r, 0, PI)

EndFunction